import time

from selenium import webdriver
from selenium.webdriver.common.by import By

from com.yh.likeshop.base.page_object import PageObject
from com.yh.likeshop.utils.utils import like_host


class CartPage(PageObject):


    #全选多选框元素
    checkAllBoxEl = (By.XPATH,'//*[@id="__layout"]/div/main/div/div/div[1]/div[1]/div[1]/label')

    # 删除按钮
    deleteEl = (By.XPATH,'//*[@id="__layout"]/div/main/div/div/div[1]/div[2]/div[1]/span/span/div')

    # + 添加数量按钮
    addEl = (By.XPATH, '//*[@id="__layout"]/div/main/div/div/div[1]/div[2]/div[1]/div[3]/div/div[2]')

    #数量输入框
    numberEl = (By.XPATH, '//*[@id="__layout"]/div/main/div/div/div[1]/div[2]/div[1]/div[3]/div/input')

    #去结算
    saveOrderEl = (By.XPATH, '//*[@id="__layout"]/div/main/div/div/div[1]/div[3]/div[2]/div[2]')

    #定义价格元素
    priceEl = (By.XPATH, '//*[@id="__layout"]/div/main/div/div/div[1]/div[2]/div[1]/div[2]')


    # 删除指定购物车商品
    def delete_cart_goods(self):
        self.driver.get(like_host + "/pc/shop_cart")

        # 获取删除按钮元素 并点击
        self.click(*self.deleteEl)



    # 输入框修改商品数量
    def updata_cart_inputNum(self,n):
        self.driver.get(like_host + "/pc/shop_cart")

        inputEl = self.findOne(*self.numberEl)
        # 清空原来的值
        inputEl.clear()
        # 输入值
        inputEl.send_keys(n)

    def checkInputNum(self,n):
        self.driver.get(like_host + "/pc/shop_cart")

        self.click(*self.priceEl)
        el = self.findOne(*self.numberEl)
        num = el.get_attribute('value')
        return num == str(n)



    # 点击+修改商品数量
    def updata_cartNumber(self,n):
        self.driver.get(like_host + "/pc/shop_cart")

        #获取+输入框元素
        addEl = self.findOne(*self.addEl)
        for i in range(n):
            time.sleep(1)
            addEl.click()

    # 提交订单
    def saveOrder(self):
        self.driver.get(like_host + "/pc/shop_cart")

        self.click(*self.saveOrderEl)


